const assert = require("assert");
const Router = require("../src/router.js");
const Ret = require("../src/ret");

describe("router.js", () => {
  describe("route()", () => {
    it("have function", () => {
      let targets = {
        "/": {
          add: function (req, res) {
            res.send("ok");
          },
        },
      };
      let req = { path: "/add" };
      let res = {
        send: function (data) {
          console.log("send: " + data);
          assert.deepStrictEqual(data, "ok");
        },
      };
      Router.route(targets, req, res);
    });
    it("no function", () => {
      let targets = {
        "/": {
          add: function (req, res) {
            res.send("ok");
          },
        },
      };
      let req = { path: "/add2" };
      let res = {
        send: function (data) {
          assert(JSON.parse(data).success == false);
        },
      };
      Router.route(targets, req, res);
    });
    it("no path", () => {
      let targets = {};
      let req = { path: "/fav.ico" };
      let res = {
        send: function (data) {
          assert(JSON.parse(data).success == false);
        },
      };
      Router.route(targets, req, res);
    });
    it("error", async () => {
      let targets = {
        "/": {
          add: function (req, res) {
            throw new Error("参数：出错啦");
          },
        },
      };
      let req = { path: "/add", headers: [] };
      let res = {
        send: function (data) {
          console.log("send: " + data);
          assert(JSON.parse(data).success == false);
        },
      };
      await Router.route(targets, req, res);
    });
    it("multi controllers", () => {
      let targets = {
        "/menu": {
          add: function (req, res) {
            res.send(new Ret(true).toString());
          },
        },
      };
      let req = { path: "/menu/add" };
      let res = {
        send: function (data) {
          console.log("send: " + data);
          assert(JSON.parse(data).success);
        },
      };
      Router.route(targets, req, res);
    });
    it("no default error format", async () => {
      let targets = {
        "/": {
          add: function (req, res) {
            throw new Error("出错啦");
          },
        },
      };
      let req = { path: "/add" };
      let res = {
        send: function (data) {
          console.log("send: " + data);
          assert(false);
        },
      };
      Router.useDefaultFormat(false);
      let result = await Router.route(targets, req, res);
      console.log("result", result);
      assert(result.indexOf("出错啦") >= 0);
    });
  });
  it("return value for send", async () => {
    let targets = {
      "/": {
        add: function (req, res) {
          return 1;
        },
      },
    };
    let req = { path: "/add" };
    let res = {
      send: function (data) {
        console.log("send: " + data);
        assert(data === "1");
      },
    };
    await Router.route(targets, req, res);
  });
  it("return string for send", async () => {
    let targets = {
      "/": {
        add: function (req, res) {
          return JSON.stringify({
            code: 1000,
            success: false,
            message: "请输入发货类型",
          });
        },
      },
    };
    let req = { path: "/add" };
    let res = {
      send: function (data) {
        console.log("send: " + data);
        assert(
          data === '{"code":1000,"success":false,"message":"请输入发货类型"}'
        );
      },
    };
    await Router.route(targets, req, res);
  });
  it("return object for send", async () => {
    let targets = {
      "/": {
        add: function (req, res) {
          return { code: 1000, success: false, message: "请输入发货类型" };
        },
      },
    };
    let req = { path: "/add" };
    let res = {
      send: function (data) {
        console.log("send: " + data);
        assert(
          data === '{"code":1000,"success":false,"message":"请输入发货类型"}'
        );
      },
    };
    await Router.route(targets, req, res);
  });
  it("return Ret for send", async () => {
    let targets = {
      "/": {
        add: function (req, res) {
          return new Ret(true);
        },
      },
    };
    let req = { path: "/add" };
    let res = {
      send: function (data) {
        console.log("send: " + data);
        assert(data === '{"success":true}');
      },
    };
    await Router.route(targets, req, res);
  });
  it("request params", async () => {
    let targets = {
      "/product": {
        add: function (req, res) {
          assert(req.params.a === 1);
          return 1;
        },
      },
    };
    let req = { method: "GET", path: "/product/add", queries: { a: 1 } };
    let res = {
      send: function (data) {
        console.log("send: " + data);
      },
    };
    await Router.route(targets, req, res);
  });
  it("compatible query", async () => {
    let targets = {
      "/product": {
        add: function (req, res) {
          assert(req.params.a === 1);
          return 1;
        },
      },
    };
    let req = { method: "GET", path: "/product/add", query: { a: 1 } };
    let res = {
      send: function (data) {
        console.log("send: " + data);
      },
    };
    await Router.route(targets, req, res);
  });

  it("before return false", async () => {
    let targets = {
      before: async () => {
        return new Ret(false, "错误", Ret.CODE_LOGIC);
      },
      "/product": {
        add: function (req, res) {
          assert(req.params.a === 1);
          return 1;
        },
      },
    };
    let req = {
      method: "GET",
      path: "/product/add",
      query: { a: 1 },
      headers: {},
    };
    let res = {
      send: function (data) {
        console.log("send: " + data);
      },
    };
    await Router.route(targets, req, res);
  });

  it("header compress", async () => {
    let targets = {
      "/": {
        add: function (req, res) {
          res.send("ok");
        },
      },
    };
    let req = { path: "/add", headers: { "accept-encoding": "br" } };
    let res = {
      send: function (data) {
        console.log("send: " + data);
      },
    };
    console.log("start");
    await Router.route(targets, req, res);
    console.log("end");
  });
});
